package com.app.controller;


import com.app.conf.RedisService;
import com.app.model.JobPublishModel;
import com.app.model.PositionModel;
import com.app.model.UserModel;
import com.app.service.JobPublishService;
import com.app.service.PositionService;
import com.app.service.UserService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RestController
public class PositionController {

    @Autowired
    private PositionService positionService;

    @Autowired
    private JobPublishService jobPublishService;

    @Autowired
    private RedisService redisService;

    @Autowired
    UserService userService;

    //查询三级职位或二级行业
    @PostMapping("findJobName")
    public Result<Object> findAll() {
        List<PositionModel> modelList = (List<PositionModel>) redisService.getStringKey("findJobNameList");
        return Result.success(modelList);
    }

    //查询三级职位或二级行业
    @PostMapping("generateFindJobName")
    public Result<Object> generateFindJobName(int postType) {
        JSONObject json = new JSONObject();
        List<PositionModel> modelList = positionService.findName(1, postType);
        if (postType != 2) {
            for (int i = 0; i < modelList.size(); i++) {
                List<PositionModel> positionList = positionService.findNameJid(modelList.get(i).getPostCode(), postType);
                modelList.get(i).setSubLevelModelList(positionList);
                for (int j = 0; j < positionList.size(); j++) {
                    List<PositionModel> positionList2 = positionService.findNameJid(positionList.get(j).getPostCode(), postType);
                    if (positionList2 != null && positionList2.size() > 0) {
                        positionList.get(j).setSubLevelModelList(positionList2);
                    }
                }
            }
        }
        redisService.deleteKey("findJobNameList");
        redisService.setStringKey("findJobNameList", modelList);

        return Result.success(null);
    }

    @GetMapping("findMktJobName")
    public Result<Object> findMktAll(int postType) {
        List<PositionModel> modelList = positionService.findName(1, postType);
        if (postType != 1) {
            for (int i = 0; i < modelList.size(); i++) {
                List<PositionModel> positionList = positionService.findNameJid(modelList.get(i).getPostCode(), postType);
                modelList.get(i).setSubLevelModelList(positionList);

            }
        }
        return Result.success(modelList);
    }

    //查询全部一级分类
    @PostMapping("findJobNameOne")
    public Result<Object> findJobNameOne() {
        List<PositionModel> modelList = positionService.findName(1, 1);
        return Result.success(modelList);
    }


    //随机匹配8个相关行业(个人端)
    @GetMapping("getMatchingIndustry")
    public Result<Object> getMatchingIndustry(HttpServletRequest request) {

        String token = request.getHeader("token");
        UserModel user = new UserModel();
        if (token == null) {
            user = null;
        }else{
            String myMobile = TokenUtils.verify(token);
            user = userService.getUserByMobile(myMobile);
        }

        return Result.success(positionService.getModelListByFather(user));
    }

    //随机匹配8个相关行业(企业端)
    @GetMapping("getMatchingIndustryCom")
    public Result<Object> getMatchingIndustryCom(HttpServletRequest request) {

        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");

        }
        String myMobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(myMobile);

        if (user == null) {
            return Result.error(405, "用户不存在");

        }
        return Result.success(positionService.getModelListByFather(user));
    }

    //随机匹配8个相关行业(企业端)
    @GetMapping("getIndustryRanking")
    public Result<Object> getIndustryRanking() {
        List<JobPublishModel> jobModel = jobPublishService.getModelGroupType();
        List<PositionModel> modelList = new ArrayList<>();
        for (int i = 0; i < jobModel.size(); i++) {
            PositionModel model= positionService.getModelById(jobModel.get(i).getJobType());
            modelList.add(model);
        }
        return Result.success(modelList);
    }
}
